package org.dant.ant.extension.net.processors;

import java.io.*;
import java.util.*;

import org.dant.ant.extension.net.Processor;
import org.dant.ant.extension.tasks.commands.CommandsConstants;
import org.dant.ant.extension.tasks.internal.RemoteCommand;
import org.dant.ant.extension.tasks.internal.RemoteResponse;
import org.dant.ant.extension.utils.FileUtil;

public class AntCallProcessor implements Processor {

    public String executeAnt(String file, String target, Map parameters ) {
        File buildFile = new File(file);
        AntCall call = new AntCall();
        call.setSysProperties(parameters);
        return call.executeTarget(buildFile, target);
    }

    public void process(RemoteCommand req, RemoteResponse response) {
        System.out.println("Ant call requied from : " + req.getSourceHost());
        response.setCommandType(req.getName());
        if (CommandsConstants.DANT_CMD_ANT_CALL.equals(req.getName())) {

            String buildFile = (String) req.getParameter("buildfile");
            String target = (String) req.getParameter("target");
            Map parameters = (Map) req.getParameter("parameters");
      
            String results = executeAnt(buildFile, target, parameters);
            response.setResult(results);
        } else {
            response.setSuccess(false);
            response.setResult("Required a ANT CALL command, but is not a ANT CALL command");
        }
    }

}
